package x.x.x;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPSocketClient {
    
    /**
     * UDPSocket通信的客户端
     *@author hanxy17
     * 如修改方法体请自行修改作者信息
     * 当前作者只对自己编写内容负责
     * 2017年3月9日下午6:10:23
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        final String serverHost = "127.0.0.1";
        final int port = 37211;
        InetAddress address = InetAddress.getByName(serverHost);
        
        byte[] buf = "服务器你收到我的消息没有?".getBytes();
        DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length,address,port);
        DatagramSocket datagramSocket = new DatagramSocket();
        
        //向服务端发送报文
        datagramSocket.send(datagramPacket);
        byte[] rb = new byte[1000];
        DatagramPacket receiveDatagramPacket = new DatagramPacket(rb, rb.length,address,port);
        datagramSocket.receive(receiveDatagramPacket);//客户端接收报文
        String content = new String(rb,0,receiveDatagramPacket.getLength());
        System.err.println("客户端收到发送服务器接的content:");
        System.err.println(content);
        datagramSocket.close();
    }
}
